package com.example.reader.ui.read.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.blankj.utilcode.utils.ScreenUtils;

/**
 * Created by Administrator on 2017/3/14.
 */

public class BookView extends View {

    private static final String TAG = "BookView";


    public int changePage = 0;

    public String text;

    private BookDrawHelper drawHelper;
    public PagerClickListenner pagerClickListenner;
    private Canvas convas;

    public BookView(Context context) {
        this(context, null);
    }

    public BookView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BookView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        drawHelper = new BookDrawHelper(context
                , ScreenUtils.getScreenWidth()
                , ScreenUtils.getScreenHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        this.convas = canvas;
        super.onDraw(canvas);
        Log.d(TAG, "onDraw() called with: canvas = [" + canvas + "]");

        if (changePage == 1){
            drawNextPage(canvas);
        }else if (changePage == -1){
            drawBeforePage(canvas);
        }else {
            drawCurrentPage(canvas);
        }
        changePage = 0;

    }

    private void drawBeforePage(Canvas canvas) {
        drawHelper.drawBeforePage(canvas);
    }

    private void drawNextPage(Canvas canvas) {
        drawHelper.drawNextPage(canvas);
    }

    private void drawCurrentPage(Canvas canvas) {
        drawHelper.drawCurrentPager(canvas);
    }

    public void showNextPage(){
        changePage = 1;
        invalidate();
    }

    public void showBeforePage(){
        changePage = -1;
        invalidate();
    }



    float downX = 0, downY = 0;
    @Override
    public boolean onTouchEvent(MotionEvent event) {


        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                if (Math.abs(downX - event.getX()) < 10 && Math.abs(downY - event.getY()) < 10) {
                    if (pagerClickListenner != null) {
                        if (((int) downX) < (getWidth() / 2)) {
                            pagerClickListenner.onClickLeft(this);
                        } else {
                            pagerClickListenner.onClickRight(this);
                        }
                    }
                }
                break;
        }

        return true;
    }

    public void setPagerClickListenner(PagerClickListenner pagerClickListenner) {
        this.pagerClickListenner = pagerClickListenner;
    }

    public static abstract class PagerClickListenner {
        public void onClickLeft(BookView bookview) {
        }

        public void onClickRight(BookView bookview) {
        }
    }

}
